/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import BL.BLPropietario;
import BL.LimitarCaracteres;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Guillermo
 */
public class UIPropietarios1 extends javax.swing.JFrame {

    /**
     * Creates new form UIPROPIETARIO
     */
    public UIPropietarios1() {
        initComponents();
        this.setLocationRelativeTo(null);
//        limitar();
        this.setLocationRelativeTo(null);
        comboavaluos.addItem("Seleccione un avaluo");
        llenarComboAvaluos();
    }

//    public void limitar() {
//        txtnombre.setDocument(new LimitarCaracteres(txtnombre, 45));
//        txtape1.setDocument(new LimitarCaracteres(txtape1, 45));
//        txtape2.setDocument(new LimitarCaracteres(txtape2, 45));
//    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnmenu = new org.edisoncor.gui.button.ButtonIcon();
        btneditar = new org.edisoncor.gui.button.ButtonIcon();
        btnconsultar = new org.edisoncor.gui.button.ButtonIcon();
        btnguardar = new org.edisoncor.gui.button.ButtonIcon();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtid = new org.edisoncor.gui.textField.TextField();
        jLabel1 = new javax.swing.JLabel();
        txtnombre = new org.edisoncor.gui.textField.TextField();
        jLabel2 = new javax.swing.JLabel();
        txtape1 = new org.edisoncor.gui.textField.TextField();
        jLabel3 = new javax.swing.JLabel();
        txtape2 = new org.edisoncor.gui.textField.TextField();
        jLabel4 = new javax.swing.JLabel();
        comboavaluos = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Propietarios");
        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(108, 131, 177));

        btnmenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Menu.png"))); // NOI18N
        btnmenu.setToolTipText("");
        btnmenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnmenuActionPerformed(evt);
            }
        });

        btneditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/editar.png"))); // NOI18N
        btneditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditarActionPerformed(evt);
            }
        });

        btnconsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Consultar.png"))); // NOI18N
        btnconsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnconsultarActionPerformed(evt);
            }
        });

        btnguardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(142, Short.MAX_VALUE)
                .add(btnguardar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btnconsultar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btneditar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(35, 35, 35)
                .add(btnmenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(130, 130, 130))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, btnmenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(btnguardar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(btnconsultar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(btneditar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(20, 20, 20))
        );

        jPanel2.setBackground(new java.awt.Color(108, 131, 177));
        jPanel2.setForeground(new java.awt.Color(0, 102, 153));

        jLabel6.setBackground(new java.awt.Color(255, 255, 255));
        jLabel6.setFont(new java.awt.Font("Cambria", 3, 24)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Ingresar Propietarios ");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(243, 243, 243)
                .add(jLabel6)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(37, 37, 37)
                .add(jLabel6)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel1.setText("Cédula");

        jLabel2.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel2.setText("Nombre");

        jLabel3.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel3.setText("Primer Apellido");
        jLabel3.setToolTipText("");

        jLabel4.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel4.setText("Segundo Apellido");

        jLabel5.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel5.setText("Avaluo");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(160, 160, 160)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jLabel2)
                        .add(jLabel3)
                        .add(jLabel4)
                        .add(jLabel5)
                        .add(jLabel1))
                    .add(51, 51, 51)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(txtid, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                        .add(txtnombre, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(txtape1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(txtape2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(comboavaluos, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(160, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 181, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(118, 118, 118)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel1)
                                .add(txtid, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(18, 18, 18)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel2)
                                .add(txtnombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(18, 18, 18)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel3)
                                .add(txtape1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(18, 18, 18)
                            .add(jLabel4))
                        .add(txtape2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel5)
                        .add(comboavaluos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(119, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnmenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnmenuActionPerformed
        // TODO add your handling code here:
        //        UIAvaluos menu = new UIAvaluos();
        //        menu.setVisible(true);
        //        this.dispose();
    }//GEN-LAST:event_btnmenuActionPerformed

    private void btneditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditarActionPerformed
        // TODO add your handling code here:
        Editar();
    }//GEN-LAST:event_btneditarActionPerformed

    private void btnconsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnconsultarActionPerformed
        buscarPropietario();
    }//GEN-LAST:event_btnconsultarActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        // TODO add your handling code here:
        insertar();
    }//GEN-LAST:event_btnguardarActionPerformed

    private void txtidKeyTyped(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        char c;
        c = evt.getKeyChar();
        if (c < '0' || c > '9') {
            evt.consume();//ignora el caracter digitado 
            if (Character.isLetter(c)) {// si es letra no lo permite
                getToolkit().beep();// al no permitirlo hace un sonido 
                JOptionPane.showMessageDialog(null, "Solo se aceptan numeros enteros", "Error al agreagar datos", JOptionPane.ERROR_MESSAGE);//mensaje informativo
            }//fin if
        }
    }

    private Boolean validarcampos() {//es para validar que todos los campos se escriban

        if (txtid.getText().trim().equals("")) {
            return false;
        }
        if (txtnombre.getText().trim().equals("")) {
            return false;
        }
        if (txtape1.getText().trim().equals("")) {
            return false;
        }
        if (txtape2.getText().trim().equals("")) {
            return false;
        }
        return true;
    }

    private void limpiacampos() {
        txtid.setText("");
        txtnombre.setText("");
        txtape1.setText("");
        txtape2.setText("");

    }

    public void buscarPropietario() {
//int cedula, String nombreb, String apel1, String apel2;
        if (txtid.getText() != "") {
            int codigo = Integer.parseInt(txtid.getText());
            BLPropietario propietario = new BLPropietario();
            try {
                if (propietario.buscarPropietario(codigo)) {
                    JOptionPane.showMessageDialog(null, "Busqueda Exitosa", "Información", JOptionPane.INFORMATION_MESSAGE);
                    txtnombre.setText(propietario.getNombre());
                    txtape1.setText(propietario.getApellido1());
                    txtape2.setText(propietario.getApellido2());
//                    comboavaluos.setSelectedIndex(Integer.parseInt(propietario.getAvaluo()));
                } else {
                    JOptionPane.showMessageDialog(null, "NO se encontro el propietario", "Información", JOptionPane.WARNING_MESSAGE);
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Esta cedula no se encuentra", "Información", JOptionPane.WARNING_MESSAGE);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Todos el campo cedula tiene que estar lleno", "Información", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void insertar() {
        if (validarcampos()) {
            Object combo = comboavaluos.getSelectedItem();
            BLPropietario propietario = new BLPropietario(Integer.parseInt(txtid.getText()), txtnombre.getText(), txtape1.getText(), txtape2.getText(), String.valueOf(combo));
            try {
                if (propietario.insertarPropietario()) {
                    JOptionPane.showMessageDialog(null, "Propietario ingresado correctamente", "Información", JOptionPane.INFORMATION_MESSAGE);
                    limpiacampos();
                } else {
                    JOptionPane.showMessageDialog(null, "No se agrego correctamente", "Información", JOptionPane.WARNING_MESSAGE);
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Este Propietario ya existe", "Información", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Todos los campos deben estar completos", "Información", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void Editar() {
        if (validarcampos()) {
            Object combo = comboavaluos.getSelectedItem();
            BLPropietario propietario = new BLPropietario(Integer.parseInt(txtid.getText()), txtnombre.getText(), txtape1.getText(), txtape2.getText(), String.valueOf(combo));
            try {
                if (propietario.editarPropietario()) {
                    JOptionPane.showMessageDialog(null, "Propietario Actualizado correctamente", "Información", JOptionPane.INFORMATION_MESSAGE);
                    limpiacampos();
                } else {
                    JOptionPane.showMessageDialog(null, "No se actualizo correctamente", "Información", JOptionPane.WARNING_MESSAGE);
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Este Propietario ya existe", "Información", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Todos los campos deben estar completos", "Información", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void llenarComboAvaluos() {
        BLPropietario ava = new BLPropietario();
        comboavaluos.setModel(ava.cargarComboAvaluos());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonIcon btnconsultar;
    private org.edisoncor.gui.button.ButtonIcon btneditar;
    private org.edisoncor.gui.button.ButtonIcon btnguardar;
    private org.edisoncor.gui.button.ButtonIcon btnmenu;
    private javax.swing.JComboBox comboavaluos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private org.edisoncor.gui.textField.TextField txtape1;
    private org.edisoncor.gui.textField.TextField txtape2;
    private org.edisoncor.gui.textField.TextField txtid;
    private org.edisoncor.gui.textField.TextField txtnombre;
    // End of variables declaration//GEN-END:variables
}
